//
//  BaseDemoVC.m
//  FilamentDemo
//
//  Created by 8000 on 2020/5/2.
//  Copyright © 2020 tencent. All rights reserved.
//

#import "BaseDemoVC.h"
#import "FilamentConvenientHeader.h"
#import "FilamentView.h"
#import "FilamentRender.h"
#import "FPSCameraController.h"
@interface BaseDemoVC ()
@property (nonatomic, strong) FPSCameraController *cameraCtrl;
@end

@implementation BaseDemoVC

- (void)viewDidLoad {
    [super viewDidLoad];
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"相机控制"
                                                                             style:UIBarButtonItemStylePlain target:self
                                                                            action:@selector(clickCameraCtrl)];
    
	CGFloat scale = UIScreen.mainScreen.scale;
	CGSize scSize = UIScreen.mainScreen.bounds.size;
	
	_drawView = [[FilamentView alloc]initWithFrame:CGRectMake(0, 0, scSize.width, scSize.height)
									  drawableSize:CGSizeMake(scSize.width * scale, scSize.height * scale)];
	[self.view addSubview:_drawView];
	
	_render = [[FilamentRender alloc]initWithViewPort:CGRectMake(0,
																 0,
																 _drawView.drawableSize.width,
																 _drawView.drawableSize.height)
                                          nativeLayer:(__bridge void *)_drawView.layer];
    
    _cameraCtrl = [[FPSCameraController alloc]initWithEngine:_render.engine
                                                      camera:_render.camera containerView:self.view];
	
	_render.camera->lookAt(float3{0,0,1}, float3{0,0,0});
    
	_render.camera->setProjection(60, _render.viewPort.size.width / _render.viewPort.size.height, 0.01, 100);
}

- (void)clickCameraCtrl{
	[_cameraCtrl present];
}

- (void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
	
    __weak BaseDemoVC *weakself = self;
    [_render startLoopRenderWithPerFrameCallback:^{
        [weakself callbackPerRenderFrame];
    }];
}

- (void)callbackPerRenderFrame{
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

@end
